package com.thinkfit.jy.fragment;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.common.lib.constant.Constants;
import com.common.lib.constant.EventBusEvent;
import com.common.lib.fragment.BaseFragment;
import com.common.lib.manager.DataManager;
import com.common.lib.mvp.contract.EmptyContract;
import com.common.lib.mvp.presenter.EmptyPresenter;
import com.thinkfit.jy.R;
import com.thinkfit.jy.activity.BrandInfoActivity;
import com.thinkfit.jy.activity.VersionActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

/**
 * 通用设置界面
 */
public class AboutSettingFragment extends BaseFragment<EmptyContract.Presenter> implements EmptyContract.View {

    ImageView ivAutomticUpdatesSwitch;
    TextView tvVersion;

    public static AboutSettingFragment getInstance(int index) {
        Bundle bundle = new Bundle();
        bundle.putInt(Constants.BUNDLE_EXTRA, index);
//        bundle.putInt(Constants.BUNDLE_EXTRA_2, type);
        AboutSettingFragment fragment = new AboutSettingFragment();
        fragment.setArguments(bundle);
        return fragment;
    }



    @Override
    protected int getLayoutId() {
        return R.layout.fragment_about_setting;
    }

    @Override
    protected void initView(@NonNull View view, @Nullable Bundle savedInstanceState) {
        setViewsOnClickListener(R.id.cl_version,R.id.iv_automtic_updates_switch,R.id.cl_device_info,R.id.cl_brand_info);
        tvVersion = view.findViewById(R.id.tv_version);
        ivAutomticUpdatesSwitch = view.findViewById(R.id.iv_automtic_updates_switch);
        initImage(DataManager.Companion.getInstance().getIsAutoUpdate());

        PackageManager packageManager = getContext().getPackageManager();
        try {
            // 获取当前应用程序的包名
            String packageName = getContext().getPackageName();

            // 根据包名获取应用程序的版本信息
            PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);

            // 输出版本号
            int versionCode = packageInfo.versionCode;
            String versionName = packageInfo.versionName;
            tvVersion.setText(getString(R.string.app_version_xxx,versionName));
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

    }



    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public void onStop() {
        super.onStop();
        System.out.println("ddddddddddddddddddddddd   AboutFragment ");
//        DeviceTypeUtils.showBackButton(getContext().getApplicationContext());
    }


    @NonNull
    @Override
    protected EmptyContract.Presenter onCreatePresenter() {
        return new EmptyPresenter(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cl_version:
                Bundle bundle = new Bundle();
                openActivity(VersionActivity.class);
                break;
            case R.id.iv_automtic_updates_switch:
                if (DataManager.Companion.getInstance().getIsAutoUpdate()) {
                    DataManager.Companion.getInstance().saveIsAutoUpdate(false);
                    initImage(false);
                } else {
                    DataManager.Companion.getInstance().saveIsAutoUpdate(true);
                    initImage(true);
                }
                break;
            case R.id.cl_brand_info:
                HashMap<String, Object> map = new HashMap<>();
                map.put(EventBusEvent.TO_FRAGMENT,5);
                EventBus.getDefault().post(map);
                break;
            case R.id.cl_device_info:
                map = new HashMap<>();
                map.put(EventBusEvent.TO_FRAGMENT,4);
                EventBus.getDefault().post(map);
                break;

        }
    }

    public void initImage(boolean isAutomticUpdates){
        if(isAutomticUpdates){
            ivAutomticUpdatesSwitch.setImageResource(R.drawable.img_set_switch_open);
        }else {
            ivAutomticUpdatesSwitch.setImageResource(R.drawable.img_set_switch_close);
        }
    }

}
